package com.ruoyi.planthopper.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.planthopper.domain.BroadcastPlanthopperInfo;
import com.ruoyi.planthopper.domain.PlanthopperNumType;
import com.ruoyi.planthopper.service.DataAnalysisPlanthopperService;
import com.ruoyi.utils.MyDateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LiSuxuan
 * @date 2021年 08月 31日 15:29
 */
@Api("数据分析接口")
@Slf4j
@RestController
@RequestMapping("/broadcast")
public class DataAnalysisPlanthopperController extends BaseController {

    @Autowired
    private DataAnalysisPlanthopperService dataAnalysisService;

    @ApiOperation(value = "播报不同时间发生量", notes = "示例:http://localhost:8180/broadcast/difTime?startTime=2021-08-30&endTime=2021-08-31")
    @GetMapping("/difTime")
    public AjaxResult insectDiffTime(String startTime, String endTime) {
        /* 计算时间间隔，按 天 存入 集合 */
        List<String> dates = MyDateUtil.calculateTimeInterval(startTime, endTime);
        if (dates == null) {
            return AjaxResult.error("输入日期有误!");
        }
        /* 存放结果数据集 */
        List<BroadcastPlanthopperInfo> statistics = new ArrayList<>();
        for (String date : dates) {
            List<String> list = dataAnalysisService.selectInsectByDay(date);
            PlanthopperNumType planthopperNumType = json2InsectType(list);
            for (String s : list) {
                log.info(s);
            }

            BroadcastPlanthopperInfo insectStatistic = new BroadcastPlanthopperInfo();
            insectStatistic.setPlanthopperNumType(planthopperNumType);
            insectStatistic.setImgNum(dataAnalysisService.selectInsectImgNum(date));
            insectStatistic.setDay(date);
            insectStatistic.setInsectTotalNumber(planthopperNumType.getLongWingedAdult() + planthopperNumType.getShortWingedAdult() + planthopperNumType.getHighInstarLarvae() + planthopperNumType.getLowInstarLarvae() + planthopperNumType.getOther());
            int average = 0;
            if (insectStatistic.getImgNum() > 0) {
                average = insectStatistic.getInsectTotalNumber() / insectStatistic.getImgNum() * 100;
            }
            insectStatistic.setAverageInsectTotalNumber(average);
            insectStatistic.setLevel(judgmentLevel(average));
            statistics.add(insectStatistic);
        }
        return AjaxResult.success(statistics);
    }


    /**
     * 将数据库中的JSON格式转换为对象格式
     * 'ccfs': 1,
     * 'glrc': 2,
     * 'dlrc': 3,
     * 'dcfs': 4,
     *
     * @param list list列表
     * @return InsectType类型
     */
    public static PlanthopperNumType json2InsectType(List<String> list) {

        int longWingedAdult = 0;

        int shortWingedAdult = 0;

        int highInstarLarvae = 0;

        int lowInstarLarvae = 0;

        int other = 0;

        for (String s : list) {
            JSONObject totalNum = JSON.parseObject(s);
            if (totalNum.containsKey("1")) {
                longWingedAdult += (Integer) totalNum.get("1");
            }
            if (totalNum.containsKey("4")) {
                shortWingedAdult += (Integer) totalNum.get("4");
            }
            if (totalNum.containsKey("2")) {
                highInstarLarvae += (Integer) totalNum.get("2");
            }
            if (totalNum.containsKey("3")) {
                lowInstarLarvae += (Integer) totalNum.get("3");
            }
            if (totalNum.containsKey("5")) {
                other += (Integer) totalNum.get("5");
            }
        }
        return new PlanthopperNumType(longWingedAdult, shortWingedAdult, highInstarLarvae, lowInstarLarvae, other);
    }

    /**
     * 判断预警等级 0是最低 4 最高
     *
     * @param quantity 数量
     * @return 等级
     */
    public static int judgmentLevel(Integer quantity) {
        if ((quantity < 1000) && (quantity > 0)) {
            return 1;
        }
        if ((quantity >= 1000) && (quantity < 3000)) {
            return 2;
        }
        if ((quantity >= 3000) && (quantity < 8000)) {
            return 3;
        }
        if (quantity >= 8000) {
            return 4;
        }
        return 0;
    }
}
